'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
const $ = dbCmd.aggregate;
const {
	getCurrentDate
} = require('common-date')
const {
	getUuid
} = require('common-uuid')
exports.main = async (event, context) => {
	//获取当前时间
	let cDate = getCurrentDate().date
	cDate.setHours(0)
	cDate.setMinutes(0)
	cDate.setSeconds(0)
	let rDate = getCurrentDate()
	let sDate = rDate.date
	sDate.setMonth(sDate.getMonth() - 12)
	sDate.setDate(1)
	sDate.setHours(0)
	sDate.setMinutes(0)
	sDate.setSeconds(0)
	let shopieReportAppidTotal = await db.collection('wmzmall-shopie-report').aggregate().match({
		ex_month_report: false 
	}).group({
		_id: '$app_id'
	}).limit(100).end();
	shopieReportAppidTotal = shopieReportAppidTotal.data
	for (var i in shopieReportAppidTotal) {
		let app_id = shopieReportAppidTotal[i]._id
		let shop_id = app_id
		let shopieMonthReport = await db.collection('wmzmall-shopie-month-report').where({
			app_id: app_id,
			shop_id: shop_id,
			date_time: dbCmd.gte(sDate.getTime()).and(dbCmd.lt(cDate.getTime()))
		}).limit(12).orderBy('date_time', 'desc').get()
		shopieMonthReport = shopieMonthReport.data
		if (shopieMonthReport.length !== 12) {
			let tDate = new Date(sDate.getTime())
			for (var i = 0; i <= 12; i++) {
				if (i > 0) {
					tDate.setDate(1)
					tDate.setMonth(tDate.getMonth() + 1)
				}
				let sYear = tDate.getFullYear()
				let sMonth = tDate.getMonth() + 1

				let tShopieMonthReport = await db.collection('wmzmall-shopie-month-report').where({
					app_id: app_id,
					shop_id: shop_id,
					year: sYear,
					month: sMonth
				}).get()
				if (tShopieMonthReport.length === 0) {
					console.log(shop_id)
					console.log(sYear)
					console.log(sMonth)
					tShopieMonthReport = tShopieMonthReport.data
					console.log(tShopieMonthReport.length)
					return
					let eDate = new Date(tDate.getTime())
					eDate.setMonth(eDate.getMonth() + 1)
					eDate.setDate(0)
					let data = []
					let totalDate = eDate.getDate()
					for (var j = 0; j < totalDate; j++) {
						data.push({
							_id: getUuid(),
							index: j,
							year: sYear,
							month: sMonth
						})
					}
					try{
						await db.collection('wmzmall-shopie-month-report').add({
							app_id: app_id,
							shop_id: app_id,
							year: sYear,
							month: sMonth,
							pay_money: 0,
							pay_count: 0,
							refound_money: 0,
							refound_count: 0,
							count: totalDate,
							money: 0,
							wechat_count: 0,
							alipay_count: 0,
							create_time: new Date().getTime(),
							date_time: tDate.getTime(),
							data: data,
							wechat_money: 0,
							alipay_money: 0,
							sm_count: 0,
							sm_money: 0,
							lj_count: 0,
							lj_money: 0,
							dd_count: 0,
							dd_money: 0,
							cz_count: 0,
							cz_money: 0,
							hy_count: 0,
							hy_money: 0
						})
					}catch(e){
						console.log(e)
					}
				}
			}
		}
		let shopieReport = await db.collection('wmzmall-shopie-report').where({
			app_id: app_id,
			shop_id: app_id,
			date_time: dbCmd.lt(cDate.getTime()),
			ex_month_report: false
		}).limit(10).orderBy('date_time', 'asc').get()
		shopieReport = shopieReport.data
		console.log(shopieReport.length)
		for (var t in shopieReport) {
			let shopie = shopieReport[t]
			let rDate = new Date(shopie.date_time)
			let sYear = rDate.getFullYear()
			let sMonth = rDate.getMonth() + 1
			let tShopieMonthReport = await db.collection('wmzmall-shopie-month-report').where({
				app_id: app_id,
				shop_id: shop_id,
				year: sYear,
				month: sMonth
			}).get()
			tShopieMonthReport = tShopieMonthReport.data[0]
			if (tShopieMonthReport) {
				let data = tShopieMonthReport.data
				data[shopie.day - 1] = shopie
				let pay_count = tShopieMonthReport.pay_count
				if (shopie.pay_count) {
					pay_count = pay_count * 1 + shopie.pay_count * 1
				}
				let refound_money = tShopieMonthReport.refound_money
				if (shopie.refound_money) {
					refound_money = refound_money * 1 + shopie.refound_money * 1
				}
				let refound_count = tShopieMonthReport.refound_count
				if (shopie.refound_count) {
					refound_count = refound_count * 1 + shopie.refound_count * 1
				}
				let money = tShopieMonthReport.money
				if (shopie.money) {
					money = money * 1 + shopie.money * 1
				}
				let wechat_count = tShopieMonthReport.wechat_count
				if (shopie.wechat_count) {
					wechat_count = wechat_count * 1 + shopie.wechat_count * 1
				}
				let alipay_count = tShopieMonthReport.alipay_count
				if (shopie.alipay_count) {
					alipay_count = alipay_count * 1 + shopie.alipay_count * 1
				}
				let wechat_money = tShopieMonthReport.wechat_money
				if (shopie.wechat_money) {
					wechat_money = wechat_money * 1 + shopie.wechat_money * 1
				}
				let alipay_money = tShopieMonthReport.alipay_money
				if (shopie.alipay_money) {
					alipay_money = alipay_money * 1 + shopie.alipay_money * 1
				}
				let sm_count = tShopieMonthReport.sm_count
				if (shopie.sm_count) {
					sm_count = sm_count * 1 + shopie.sm_count * 1
				}
				let sm_money = tShopieMonthReport.sm_money
				if (shopie.sm_money) {
					sm_money = sm_money * 1 + shopie.sm_money * 1
				}
				let lj_count = tShopieMonthReport.lj_count
				if (shopie.lj_count) {
					lj_count = lj_count * 1 + shopie.lj_count * 1
				}
				let lj_money = tShopieMonthReport.lj_money
				if (shopie.lj_money) {
					lj_money = lj_money * 1 + shopie.lj_money * 1
				}
				let dd_count = tShopieMonthReport.dd_count
				if (shopie.dd_count) {
					dd_count = dd_count * 1 + shopie.dd_count * 1
				}
				let dd_money = tShopieMonthReport.dd_money
				if (shopie.dd_money) {
					dd_money = dd_money * 1 + shopie.dd_money * 1
				}
				let cz_count = tShopieMonthReport.cz_count
				if (shopie.cz_count) {
					cz_count = cz_count * 1 + shopie.cz_count * 1
				}
				let cz_money = tShopieMonthReport.cz_money
				if (shopie.cz_money) {
					cz_money = cz_money * 1 + shopie.cz_money * 1
				}
				let hy_count = tShopieMonthReport.hy_count
				if (shopie.hy_count) {
					hy_count = hy_count * 1 + shopie.hy_count * 1
				}
				let hy_money = tShopieMonthReport.hy_money
				if (shopie.hy_money) {
					hy_money = hy_money * 1 + shopie.hy_money * 1
				}
				await db.collection('wmzmall-shopie-month-report').doc(tShopieMonthReport._id).update({
					pay_money: money - refound_money,
					pay_count: pay_count,
					refound_money: refound_money,
					refound_count: refound_count,
					money: money,
					alipay_count: alipay_count,
					wechat_count: wechat_count,
					data: data,
					wechat_money: wechat_money,
					alipay_money: alipay_money,
					sm_count: sm_count,
					sm_money: sm_money,
					lj_count: lj_count,
					lj_money: lj_money,
					dd_count: dd_count,
					dd_money: dd_money,
					cz_count: cz_count,
					cz_money: cz_money,
					hy_count: hy_count,
					hy_money: hy_money
				})
				await db.collection('wmzmall-shopie-report').doc(shopie._id).update({
					ex_month_report: true
				})
			}
		}
	}

	/* let shopieReport = await db.collection('wmzmall-shopie-report').where({
		q_month_report: dbCmd.exists(false)
	}).limit(100).orderBy('create_time', 'desc').get()
	shopieReport = shopieReport.data
	//获取当前时间
	let cDate = getCurrentDate().date
	let rDate = getCurrentDate()
	let sDate = rDate.date
	sDate.setMonth(sDate.getMonth() - 12)
	sDate.setDate(1)
	sDate.setHours(0)
	sDate.setMinutes(0)
	sDate.setSeconds(0)

	if (shopieReport.length > 0) {
		for (var i in shopieReport) {
			let app_id = shopieReport[i].app_id
			let shop_id = app_id
			let shopieMonthReport = await db.collection('wmzmall-shopie-month-report').where({
				app_id: app_id,
				shop_id: shop_id,
				date_time: dbCmd.gte(sDate.getTime()).and(dbCmd.lt(cDate.getTime()))
			}).limit(12).orderBy('date_time', 'desc').get()
			shopieMonthReport = shopieMonthReport.data
			let tDate = new Date(sDate.getTime())
			if (shopieMonthReport.length !== 12) {
				for (var i = 0; i <= 12; i++) {
					if (i > 0) {
						tDate.setDate(1)
						tDate.setMonth(tDate.getMonth() + 1)
					}
					let sYear = tDate.getFullYear()
					let sMonth = tDate.getMonth() + 1

					let tShopieMonthReport = await db.collection('wmzmall-shopie-month-report').where({
						app_id: app_id,
						shop_id: shop_id,
						year: sYear,
						month: sMonth
					}).get()
					tShopieMonthReport = tShopieMonthReport.data
					if (tShopieMonthReport.length === 0) {
						let eDate = new Date(tDate.getTime())
						eDate.setMonth(eDate.getMonth() + 1)
						eDate.setDate(0)
						 let data = []
						let totalDate = eDate.getDate()
						for (var j = 0; j < totalDate; j++) {}
						await db.collection('wmzmall-shopie-month-report').add({
							app_id: app_id,
							shop_id: app_id,
							year: sYear,
							month: sMonth,
							pay_money: 0,
							pay_count: 0,
							refound_money: 0,
							refound_count: 0,
							count: totalDate,
							money: 0,
							create_time: new Date().getTime(),
							date_time: tDate.getTime(),
							data: data
						})
					}
				}
			} else {
				let shopie = shopieReport[i]
				let rDate = new Date(shopie.date_time)
				let sYear = rDate.getFullYear()
				let sMonth = rDate.getMonth() + 1

				let tShopieMonthReport = await db.collection('wmzmall-shopie-month-report').where({
					app_id: app_id,
					shop_id: shop_id,
					year: sYear,
					month: sMonth
				}).get()
				tShopieMonthReport = tShopieMonthReport.data[0]
				if (tShopieMonthReport) {
					let data = tShopieMonthReport.data
					data.push(shopie)
					data.sort((a, b) => (a.date_time > b.date_time) ? 1 : -1); 
					let pay_count = tShopieMonthReport.pay_count
					if (shopie.pay_count) {
						pay_count = pay_count + shopie.pay_count
					}
					let refound_money = tShopieMonthReport.refound_money
					if (shopie.refound_money) {
						refound_money = refound_money + shopie.refound_money
					}
					let refound_count = tShopieMonthReport.refound_count
					if (shopie.refound_count) {
						refound_count = refound_count + shopie.refound_count
					}
					let money = tShopieMonthReport.money
					if (shopie.money) {
						money = money + shopie.money
					}
					await db.collection('wmzmall-shopie-month-report').doc(tShopieMonthReport._id).update({
						pay_money: money-refound_money,
						pay_count: pay_count,
						refound_money: refound_money,
						refound_count: refound_count,
						money: money,
						data: data
					})
					await db.collection('wmzmall-shopie-report').doc(shopie._id).update({
						q_month_report: true
					})
				}
			}
		}
	} */
	return event
};